package de.tabuma.ssoclient.logik;

import java.util.*;
import de.tabuma.ssoclient.entity.Privileg;

/**
 *
 * @author alefen
 */
public class SSOHashFacade {
    private static Hashtable<String, String> ssoHash = new Hashtable<String, String>();
    private static Hashtable<String, Date> dateHash = new Hashtable<String, Date>();
    private static Hashtable<String, List<Privileg>> privilegHash = new Hashtable<String, List<Privileg>>();

    public static synchronized void put(String user, String ssoId) {
        ssoHash.put(user, ssoId);
        dateHash.put(ssoId, Calendar.getInstance().getTime());
    }

    public static synchronized String get(String user) {
        String ssoId = ssoHash.get(user);
        if(ssoId==null) {
            return null;
        }
        Date date = dateHash.get(ssoId);

        Date currentDate = Calendar.getInstance().getTime();

        long diff = currentDate.getTime() - date.getTime();

        //Millis->Seconds/Seconds->Minutes
        diff = diff/1000/60;
        if(diff<25) {
            return ssoId;
        }
        return null;
    }

    public static synchronized List<Privileg> getPrivilegList(String ssoId) {
        Date date = dateHash.get(ssoId);
        if(date==null) {
            return null;
        }
        Date currentDate = Calendar.getInstance().getTime();
        long diff = currentDate.getTime() - date.getTime();

        //Millis->Seconds/Seconds->Minutes
        diff = diff/1000/60;
        if(diff<25) {
            return privilegHash.get(ssoId);
        }
        return null;
    }

    public static synchronized void setPrivilegList(List<Privileg> pList, String ssoId) {
        privilegHash.put(ssoId, pList);
    }

    public static synchronized void clearPrivilegList() {
        Enumeration<String> enu = privilegHash.keys();
        while(enu.hasMoreElements()) {
            String key = enu.nextElement();
            privilegHash.remove(key);
        }
    }
}